<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles-extras" prefix="tilesx" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>

<script type="text/javascript">

function removeError(itemName)
{
	  var itemId = parseIdForJQuery(itemName);

	  if ($(itemId + "_errorField").length > 0)
	  {
		  $("#" + itemName + "_errorField").remove();
		  $("#errorCount").val(parseInt(parseInt($("#errorCount").val()) - parseInt(1)));
	  }
}

function addError(itemName, errorMessage)
{
  var itemId = parseIdForJQuery(itemName);
  if ($(itemId + "_errorField").length < 1)
  {
	$("#errorsTable").css("display", "block");
	$("#errorCount").val(parseInt(parseInt($("#errorCount").val()) + parseInt(1)));
	$("#errorsTable").html($("#errorsTable").html() + "<tr id='" + itemName + "_errorField' class='errorText'><td>" + errorMessage + "</td>");
	disableButton("saveButton");
  }
  else
  {
	if ($(itemId + "_errorField").css("visibility") == "hidden")
	{
	  $("#errorCount").val(parseInt(parseInt($("#errorCount").val()) + parseInt(1)));
	  $(itemId + "_errorField").css("visibility", "visible").css("display" , "block").html(errorMessage);
      $(itemId).attr("title", errorMessage);
	  disableButton("saveButton");
    }
  }
}


//////////////////////////////////////////////////////////////////////////////////////////
function openSaveChangesDialog()
{
	afterFieldEdit('<spring:message code="field.existing.password"/>', 'string', 'existingPassword', 'false', new Array());
	afterFieldEdit('<spring:message code="field.new.password"/>', 'string', 'newPassword', 'false', new Array());
	afterFieldEdit('<spring:message code="field.confirm.password"/>', 'string', 'newPasswordConfirmation', 'false', new Array());
	setTimeout("checkErrorsForSaveChangesDialog()", 100);
}

function removeSaveMessage()
{
	$('.successfulSave').remove();
}

function validateExistingPassword(itemName)
{
  removeSaveMessage();
  var userNameField = document.getElementById('adminCommand.username');
  var newValueField = document.getElementById(itemName);
  console.log("validate existing password ... ");

  var callStatus = document.getElementById("callStatus").value;

  console.log("validateExistingPassword. callStatus [" + callStatus + "] . Type-> " + typeof callStatus);

  if (callStatus === "ERROR")
  {
	// stop processing
	console.log("Validate Existing password. ["+callStatus+"] Error set so processing stops ... ");
	return;
  }

  $.ajax({url: 'validation.htm', type: 'POST',
	      data : {userNameField : userNameField.value,
		    	  newValueField : newValueField.value,
    		      adminRequestedAction:"validateExistingPassword"}}).success( function (aRes) {    errorMessage = aRes;

																	    		  if (errorMessage != "")
																	    		  {
																	    			console.log("Validate Existing Password. ERROR DETECTED: " + errorMessage + ", newValueField: " + newValueField);
																	    		    changeClassToError(newValueField);
																	    		    addError(itemName, errorMessage);
																	    		  }
																	    		  else
																	    		  {
																	    		    fieldEdit(itemName);
																	    		    removeError(itemName);
																	    		  }
    		  													});
}

function validatePassword(itemName)
{
  var newValueField = document.getElementById(itemName);
  var userName = document.getElementById("adminCommand.username");

  $.ajax({url: 'validation.htm', type: 'POST',
      	  data : {newValue : newValueField.value,
	    		  userName : userName.value,
		      	  adminRequestedAction:"validatePassword"}}).success( function (aRes) {    errorMessage = aRes;
																    		  if (errorMessage != "")
																    		  {
																    		    changeClassToError(newValueField);
																    		    addError(itemName, errorMessage);
																    		  }
																    		  else
																    		  {
																    		    fieldEdit(itemName);
																    		    removeError(itemName);
																    		  }

																    		  // Clear the new password confirmation
																    		  var newPasswordConfirmationField = document.getElementById('newPasswordConfirmation');
																    		  newPasswordConfirmationField.value = '';
																    		  fieldEdit('newPasswordConfirmation');
																    		  removeError('newPasswordConfirmation');
		  													});
}

function validatePasswordConfirm(itemName, passwordItemName)
{
	var newValueField = document.getElementById(itemName);
	var passwordValueField = document.getElementById(passwordItemName);

	$.ajax({url: 'validation.htm', type: 'POST',
    	  data : {newValue : newValueField.value,
	    		  passwordValue : passwordValueField.value,
		      	  adminRequestedAction:"validatePasswordConfirm"}}).success( function (aRes) {    errorMessage = aRes;
																    		  if (errorMessage != "")
																    		  {
																    		    changeClassToError(newValueField);
																    		    addError(itemName, errorMessage);
																    		  }
																    		  else
																    		  {
																    		    fieldEdit(itemName);
																    		    removeError(itemName);
																    		  }

																    		  checkButtons();
		  													});
}
</script>

<form id="adminForm" name="adminForm" method="post" commandName="adminCommand">

  <form:hidden path="adminCommand.adminRequestedAction" id="adminCommand.adminRequestedAction"/>
  <form:hidden path="adminCommand.successfulSave" id="adminCommand.successfulSave"/>
  <form:hidden path="adminCommand.username" id="adminCommand.username"/>

  <input type="hidden" id="callStatus" name="callStatus" value=""/>

  <tag:errors name="adminCommand"/>

  <c:if test="${adminCommand.successfulSave}">
  <div class="successfulSave">Successful Save.</div>
  </c:if>

  <table align="center">

    <jsp:include page="/pages/jsp/tiles/admin/errors.jsp"/>

    <tr>
      <td>
        <table class="adminEdit">
          <tr>
            <th align="center" width="150px"><spring:message code="field.existing.password"/></th>
            <td width="280px">
              <spring:bind path="adminCommand.existingPassword">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="password"
                     id="${status.expression}"
                     name="${status.expression}"
                     value="${status.value}"
                     class="largeTextField"
                     maxlength="30"
                     onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.existing.password"/>', 'string', '${status.expression}', 'false', new Array());validateExistingPassword('${status.expression}');"/>&nbsp;*
                     <%-- onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.existing.password"/>', 'string', '${status.expression}', 'false', new Array());"/>&nbsp;* --%>
              </spring:bind>
            </td>
          </tr>
          <tr>
            <th align="center" width="150px"><spring:message code="field.new.password"/></th>
            <td width="280px">
              <spring:bind path="adminCommand.newPassword">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="password"
                     id="${status.expression}"
                     name="${status.expression}"
                     value="${status.value}"
                     class="largeTextField"
                     maxlength="30"
                     onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.new.password"/>', 'string', '${status.expression}', 'false', new Array());validatePassword('${status.expression}');"/>&nbsp;*
              </spring:bind>
            </td>
          </tr>
          <tr>
            <th align="center" width="150px"><spring:message code="field.confirm.password"/></th>
            <td width="280px">
              <spring:bind path="adminCommand.newPasswordConfirmation">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="password"
                     id="${status.expression}"
                     name="${status.expression}"
                     value="${status.value}"
                     class="largeTextField"
                     maxlength="30"
                     onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.confirm.password"/>', 'string', '${status.expression}', 'false', new Array());validatePasswordConfirm('newPasswordConfirmation', 'newPassword');"/>&nbsp;*
              </spring:bind>
            </td>
          </tr>
        </table>
      </td>
    </tr>

    <tr>
      <td>
        <table align="center" style="margin-top:10px;">
          <tr>
            <td><jsp:include page="/pages/jsp/tiles/admin/changePasswordButtons.jsp"/></td>
          </tr>
        </table>
      </td>
    </tr>

    <jsp:include page="/pages/jsp/tiles/admin/hoverConfirmation.jsp" />

  </table>


</form>

